#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2023, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

###############################################################################
# This script is used to get a free IP address (or set of IPs). The IP will be
# used by OpenNebula VMs and should not be allocated to any other host in the
# network.
#
# STDIN Input:
#  - Base64 encoded XML with the AR description and the address request
#
# XML format
#  <IPAM_DRIVER_ACTION_DATA>
#    <AR>
#      <DEPLOY_ID>allocation id</DEPLOY_ID>
#      <EC2_ACCESS_KEY>access key</EC2_ACCESS_KEY>
#      <EC2_SECRET_KEY>secret key</EC2_SECRET_KEY>
#      <EC2_REGION>region</EC2_REGION>
#    </AR>
#    <ADDRESS>
#      <IP>
#        <SIZE> Number of IPs to allocate</SIZE>
#      </IP>
#    </ADDRESS>
#  </IPAM_DRIVER_ACTION_DATA>
#
# This scrit MUST output the leased IP range, if the "size" IPs cannot be
# assgined the sript must return -1, otherwise it must exit 0. The answer to
# OpenNebula needs to include the ADDRESS spec:
#
#  ADDRESS = [ IP = "10.0.0.2", SIZE=34 ]
#
################################################################################

ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION)

if !ONE_LOCATION
    RUBY_LIB_LOCATION   ||= '/usr/lib/one/ruby'
    GEMS_LOCATION       ||= '/usr/share/one/gems'
else
    RUBY_LIB_LOCATION   ||= ONE_LOCATION + '/lib/ruby'
    GEMS_LOCATION       ||= ONE_LOCATION + '/share/gems'
end

# %%RUBYGEMS_SETUP_BEGIN%%
if File.directory?(GEMS_LOCATION)
    real_gems_path = File.realpath(GEMS_LOCATION)
    if !defined?(Gem) || Gem.path != [real_gems_path]
        $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }

        # Suppress warnings from Rubygems
        # https://github.com/OpenNebula/one/issues/5379
        begin
            verb = $VERBOSE
            $VERBOSE = nil
            require 'rubygems'
            Gem.use_paths(real_gems_path)
        ensure
            $VERBOSE = verb
        end
    end
end
# %%RUBYGEMS_SETUP_END%%

$LOAD_PATH << RUBY_LIB_LOCATION

require 'base64'
require 'nokogiri'

begin
    data = Nokogiri::XML(Base64.decode64(STDIN.read))

    size = data.xpath('//ADDRESS/SIZE').text
    ip   = data.xpath('//AR/IP').text

    if size.to_i != 1
        STDERR.puts 'Only IP requests of size 1 are supported'
        exit(-1)
    end

    if ip.empty?
        STDERR.puts 'Empty IP address in request'
        exit(-1)
    end

    puts <<-EOF
        ADDRESS = [
            IP   = "#{ip}",
            SIZE = "1"
        ]
    EOF
rescue StandardError => e
    STDERR.puts e.to_s
    exit(-1)
end
